import QtQuick
import Qt5Compat.GraphicalEffects

import Fk
import Fk.Components.Common
import Fk.Widgets as W

Item {
  property bool enabled: true
  property alias text: title.text
  property alias textColor: title.color
  property alias textFont: title.font
  property alias source: buttonBg.source

  property alias glow: title.glow

  property bool hovered: false

  id: root
  height: buttonBg.height
  width: buttonBg.width

  Image {
    id: buttonBg
    source: Cpp.path + "/packages/lunarltk-qsgs-ui/image/selfArea/choice-button.png"
    fillMode: Image.PreserveAspectFit
    width: sourceSize.width * 0.5
    height: sourceSize.height * 0.5
  }

  GlowText {
    id: title
    font.pixelSize: 16
    color: '#fff8d2'
    font.bold: true
    glow.color: '#000000'
    glow.spread: 0.9
    glow.radius: 2.5
    x: parent.width / 2 - (width - (font.letterSpacing * (text.length - 1))) / 2
    y: parent.height / 2 - height / 2 - 1
  }

  Rectangle {
    id: buttonHoverFilter
    anchors.fill: buttonBg
    color: '#ffffff'
    opacity: root.hovered && root.enabled ? 0.25 : 0.0
    radius: 2
  }

  signal clicked
  signal rightClicked

  W.TapHandler {
    id: mouse
    acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.NoButton
    gesturePolicy: TapHandler.WithinBounds

    onTapped: (p, btn) => {
      if (parent.enabled) {
        if (btn === Qt.LeftButton || btn === Qt.NoButton) {
          parent.clicked();
        } else if (btn === Qt.RightButton) {
          parent.rightClicked();
        }
      }
    }

    onLongPressed: {
      parent.rightClicked();
    }
  }

  HoverHandler {
    id: hover
    cursorShape: parent.enabled ? Qt.PointingHandCursor : Qt.ArrowCursor
    onHoveredChanged: {
      if (hovered) {
        root.hovered = true;
      } else {
        root.hovered = false;
      }
    }
  }

  Desaturate {
    anchors.fill: root
    source: root
    desaturation: 1.0   // 0.0 = 原色，1.0 = 黑白
    visible: !root.enabled
  }
}